package bm.com.project.server.unit.service.impl;

import bm.com.framework.security.util.SecurityUtils;
import bm.com.project.server.common.dto.DemandsSearchDTO;
import bm.com.project.server.common.dto.ObtainBaseDTO;
import bm.com.project.server.common.entity.BmAdjunctsEntity;
import bm.com.project.server.common.service.BmAdjunctsService;
import bm.com.project.server.unit.convent.DemandsConvent;
import bm.com.project.server.unit.dto.BmDemandsDTO;
import bm.com.project.server.unit.dto.BmDemandsListDTO;
import bm.com.project.server.unit.entity.BmDemandsEntity;
import bm.com.project.server.unit.mapper.BmDemandsMapper;
import bm.com.project.server.unit.service.BmDemandsService;
import bm.com.framework.common.resp.PageVO;
import bm.com.framework.common.resp.ResultVO;
import bm.com.framework.common.unit.DateUnit;
import bm.com.framework.common.unit.Tool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;


@Service("bmDemandsService")
public class BmDemandsServiceImpl extends ServiceImpl<BmDemandsMapper, BmDemandsEntity> implements BmDemandsService {

    @Resource
    BmDemandsMapper bmDemandsMapper;

    @Resource
    BmAdjunctsService bmAdjunctsService;

    /**
     * 添加或者修改需求
     *
     * @param demands
     * @return
     */
    @Override
    public ResultVO saveDemands(BmDemandsDTO demands) {

        int result = 0;

        /**
         * 进行VO和entity的映射
         */
        BmDemandsEntity entity = DemandsConvent.INSTANCE.convent(demands);

        if (demands.getAdjunctsList().size() > 0 && demands.getAdjunctsList() != null) {
            bmAdjunctsService.saveBatchAdjunct(demands.getAdjunctsList());
        }

        /**  如果是新增的话，则设置一些初始化值信息  */
        if (entity.getId().isEmpty() || entity.getId().isBlank()) {
            entity.setId(Tool.iDGenerator());
            entity.setOrgId(SecurityUtils.getOrgId());
            entity.setCreator(SecurityUtils.getLoginUserId());
            entity.setCreateDate(DateUnit.getNowTimeSecond());

            result = bmDemandsMapper.insert(entity);
        } else {
            result = bmDemandsMapper.updateById(entity);
        }

        if (result > 0) {
            return ResultVO.success("创建成功");
        }
        return ResultVO.error("创建失败");
    }

    /**
     * 获取需求列表
     *
     * @param demandsSearchDTO
     * @return
     */
    @Override
    public ResultVO getDemandsList(DemandsSearchDTO demandsSearchDTO) {

        demandsSearchDTO.setOrgId(SecurityUtils.getOrgId());
        int count = bmDemandsMapper.getDemandsList(demandsSearchDTO).size();

        if (demandsSearchDTO.getPageNum() <= 1) {
            demandsSearchDTO.setPageNum(1);
        }

        demandsSearchDTO.setPageOffset((demandsSearchDTO.getPage() - 1) * demandsSearchDTO.getPageNum());
        demandsSearchDTO.setOrgId(SecurityUtils.getOrgId());
        List<BmDemandsListDTO> memberList = bmDemandsMapper.getDemandsList(demandsSearchDTO);
        PageVO<List> PageVO = new PageVO<>(count, memberList);
        return ResultVO.success(PageVO);

    }

    /**
     * 获取详情
     *
     * @param obtainBaseDTO
     * @return
     */
    @Override
    public ResultVO getDemandsDetail(ObtainBaseDTO obtainBaseDTO) {
        BmDemandsEntity entity = bmDemandsMapper.selectById(obtainBaseDTO.getId());

        if (Objects.isNull(entity)) {
            return ResultVO.error("查无该数据");
        }

        /** 获取附件信息 */
        List<BmAdjunctsEntity> list = bmAdjunctsService.getListByType(entity.getId(), 1);

        HashMap<String, Object> obj = new HashMap<>();
        obj.put("detail", entity);
        obj.put("adjuncts", list);

        return ResultVO.success(obj);
    }
}